package com.example.demo.controller;

import com.example.demo.domain.category.Category;
import com.example.demo.domain.category.CategorySortService;
import com.example.demo.domain.category.repository.CategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 * 应用分类的控制器
 *
 * @author yingjianhua
 */
public class AppCategoryController {

//    @Autowired
//    private Category.AppCategoryFactory appCategoryFactory;
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private CategorySortService sortService;

    public void add(String name) throws Exception {
        new Category(name);
//        appCategoryFactory.create(name);
    }

    public void modify(String name, String newName) throws Exception {
        final Category category = categoryRepository.findByName(name).orElseThrow(() -> new RuntimeException("数据不存在"));
        category.setName(newName);
    }

    public void delete(String name) throws Exception {
        categoryRepository.delete(categoryRepository.findByName(name).orElseThrow(() -> new RuntimeException("数据不存在")));
    }

    public void sort(Set<String> names) throws Exception {
        sortService.sortByName(names);
    }

    public void turnEnable(String name) throws Exception {
        final Category category = categoryRepository.findByName(name).orElseThrow(() -> new RuntimeException("数据不存在"));
        category.setEnable(!category.getEnable());
    }
}
